package service

import (
	"encoding/json"

	"task/ecode"
	"task/models"

	log "github.com/sirupsen/logrus"
)

// generateGraph generate Graph.
func generateGraph(dagConfig string) (models.Graph, error) {
	var err error
	config := new(models.DagConfig)
	if err = json.Unmarshal([]byte(dagConfig), &config); err != nil {
		log.Errorf("DAG config Unmarshal error(%v)", err)
		return nil, ecode.DataError
	}
	dag := make(models.Graph)
	for _, processorId := range config.Processors {
		dag.AddNode(processorId)
	}
	for _, relations := range config.Relations {
		dag.AddEdge(relations)
	}
	return dag, nil
}
